package com.work.dao;

import com.work.dto.Goods;
import com.work.dto.Orders;
import com.work.dto.User;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.UUID;

public class OrdersDao {
    //订单的集合
    static ArrayList<Orders> orders = new ArrayList<Orders>();
    //商品的集合
    static ArrayList<Goods> goods = new ArrayList<Goods>();
    //用户的集合
    static ArrayList<User> users = new ArrayList<User>();

    Scanner sc = new Scanner(System.in);

    static {
        goods.add(new Goods(1001,"娃哈哈","饮料",8));
        goods.add(new Goods(1002,"红牛","饮料",6));
        goods.add(new Goods(1003,"加多宝","饮料",3.5));
        goods.add(new Goods(1004,"卫龙","零食",3));
        goods.add(new Goods(1005,"薯片","零食",10));
        goods.add(new Goods(1006,"篮球","球类",100));

        users.add(new User(1,"蔡徐坤","13233333333","123456","北京朝阳"));
        users.add(new User(2,"张无忌","13233333334","123456","湖北十堰武当山"));
    }

    /**
     * 添加订单
     */
    public void add(){
        System.out.println("输入订单时间:");
        String time = sc.next();
        String id = UUID.randomUUID().toString();
        //定义一个对象用来存储找到的用户数据
        User user = null;
        //定义一个商品对象存储用户购买的商品
        Goods good = null;

        //对用户进行遍历,然后将用户信息筛选出来放入到订单对象中
        for(int i = 0; i < users.size(); i++){
            if("张无忌".equals(users.get(i).getName())){
                user = users.get(i);
            }
        }

        //对商品进行遍历将商品信息取出
        System.out.println("我们的商品有:");
        for(int i = 0; i < goods.size(); i++){
            System.out.println("名称:" + goods.get(i).getName() + "\t价格:" + goods.get(i).getPrice());
        }
        System.out.println("你要买啥?");
        String g = sc.next();
        for(int i = 0; i < goods.size(); i++){
            if(g.equals(goods.get(i).getName())){
                good = goods.get(i);
            }
        }
        if(user != null && good != null){
            Orders order = new Orders(id,time,good,user);
            //向订单集合中添加数据
            orders.add(order);
        }else{
            System.out.println("订单产生失败");
        }
        System.out.println("---------------------------------------");
    }



    //根据手机号查询订单
    public void findByPhone(){
        System.out.println("输入你的手机号:");
        String phone = sc.next();
        int count = 0;
        for(int i = 0; i < orders.size(); i++){
            Orders order = orders.get(i);
            User user = order.getUser();
            String p = user.getPhone();
            if(phone.equals(p)){
                System.out.println(orders.get(i));
                count++;
            }
        }
        System.out.println(phone + "一共有" + count + "个订单");
        System.out.println("---------------------------------------");
    }

    public void findByGName(){
        System.out.println("输入商品名称:");
        String gname = sc.next();
        for(int i = 0; i < orders.size(); i++){
            if(gname.equals(orders.get(i).getGoods().getName())){
                System.out.println(orders.get(i));
            }
        }
        System.out.println("---------------------------------------");
    }
}
